package com.jieshuibao.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.model.domain.CookieUser;
import com.jieshuibao.model.domain.Integrals;
import com.jieshuibao.service.IntegralService;

/**
 * @author :liuxb@jieshuibao.com
 * @version ：2018年8月21日 下午4:00:38
 * 类说明
 */
@Controller
@RequestMapping("/integral")
public class IntegralController {
	
	@Resource
	private IntegralService integralService;
	
	
	/**
	 * 用户积分记录
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/user/integral",method=RequestMethod.POST)
	@ResponseBody
	public Object userIntegral(HttpServletRequest request){
		BaseRes<Integrals> res = new BaseRes<Integrals>();
		
		try {
			CookieUser cookieUser = (CookieUser)request.getAttribute("cookieUser");
			String phoneNumber = cookieUser.getPhoneNumber();
			
			Integrals integrals = integralService.getIntegralsByUuid(phoneNumber);
			if (integrals==null) {
				integralService.integralRegist(phoneNumber);
				integrals= integralService.getIntegralsByUuid(phoneNumber);
			}
			res.setCode(BaseRes.SUCCESS_CODE);
			res.setDes(BaseRes.SUCCESS_DES);
			res.setData(integrals);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			res.setCode(BaseRes.FAIL_CODE);
			res.setDes(BaseRes.FAIL_DES);
			return res;
		}
		
	}

}
